<script>
import { mapGetters } from 'vuex'
import EntForm from '@/views/park/enterprise/components/entForm/entForm.vue'
import EntDetail from '@/views/park/enterprise/components/entDetail/entDetail.vue'
import ContractTable from '@/views/park/enterprise/components/contractTable/contractTable.vue'
import { fetchCurrentContractAPI } from '@/api/park/enterpriseApi'

export default {
  name: 'enterprise',
  components: { ContractTable, EntDetail, EntForm },
  data() {
    return {
      /* 获取企业列表的分页参数 */
      pageParams: {
        // 第几页
        page: 1,
        // 展示数据条数
        pageSize: 10,
        // 企业名称模糊搜索
        name: undefined
      },
      /* 是否加载企业状态 */
      loading: false,
      /* 是否显示添加或者编辑企业的抽屉 */
      drawerVisible: false,
      /* 编辑企业还是添加企业 */
      edit: false,
      /*  */
      showEntDetail: false,
      /* 企业id */
      enterpriseId: ''
    }
  },
  methods: {
    /* 获取企业列表的回调函数 */
    async fetchEnterpriseList(pageParams) {
      try {
        this.loading = true
        await this.$store.dispatch('enterpriseStore/fetchEnterpriseList', pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /* 分页器size发生变化的回调函数 */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchEnterpriseList(this.pageParams)
    },
    /* 分页器page发生变化的回调函数 */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchEnterpriseList(this.pageParams)
    },
    /* 点击查询按钮的回调函数 */
    handleSearch() {
      this.pageParams.page = 1
      this.fetchEnterpriseList(this.pageParams)
    },
    /* 点击删除企业按钮的回调函数 */
    async handleDelete(row) {
      const confirm = await this.$confirm(`确定删除${row.name}吗?是否继续`, '提示').catch(error => error)
      if (confirm === 'cancel') return
      try {
        const deleteResult = await this.$store.dispatch('enterpriseStore/deleteEnterprise', row.id)
        await this.fetchEnterpriseList()
        this.$message.success(deleteResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 点击添加企业按钮的回调函数 */
    handleAdd() {
      this.edit = false
      this.drawerVisible = true
    },
    /* 点击编辑企业按钮的回调函数 */
    handleEdit(row) {
      this.edit = true
      this.drawerVisible = true
      this.$nextTick(() => {
        this.$refs.entFormRef.findEnterprise(row.id)
      })
    },
    /* 点击查看按钮的回调函数 */
    handleShow(row) {
      this.showEntDetail = true
      this.$nextTick(() => {
        this.$refs.entDetailRef.findEnterprise(row.id)
      })
    },
    /* ent-form子组件添加企业或者是编辑企业成功的回调函数 */
    handleSuccess() {
      this.drawerVisible = false
      this.fetchEnterpriseList(this.pageParams)
    },
    /* 获取当前企业租赁列表的回调函数 */
    async fetchCurrentContract(id) {
      await this.$store.dispatch('enterpriseStore/fetchCurrentContract', id)
    },
    /* 表格某一行展开或者关闭的回调函数 */
    async expandChange(expandedRow, expanded) {
      const enterprise = expanded.find(item => item.id === expandedRow.id)
      if (!enterprise) return
      try {
        this.enterpriseId = expandedRow.id
        this.fetchCurrentContract(expandedRow.id)
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  },
  computed: {
    ...mapGetters('enterpriseStore', ['enterpriseList', 'total']),
    drawerTitle() {
      return this.edit ? '编辑企业' : '添加企业'
    }
  },
  mounted() {
    this.fetchEnterpriseList(this.pageParams)
  }
}
</script>

<template>
  <div class="enterprise">
    <el-card shadow="never">
      <el-form inline :disabled="showEntDetail">
        <el-form-item label="企业名称">
          <el-input size="small" placeholder="请输入企业名称" v-model="pageParams.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never" v-if="!showEntDetail">
      <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">添加企业</el-button>
      <el-table border stripe :header-cell-style="{backgroundColor:'#f4f6f8'}" size="small" :data="enterpriseList" v-loading="loading" element-loading-text="拼命加载中" element-loading-background="rgb(255, 255, 255)" @expand-change="expandChange">
        <el-table-column type="expand">
          <template #default="{row}">
            <contract-table :list="row.list" @success="fetchCurrentContract(enterpriseId)"></contract-table>
          </template>
        </el-table-column>
        <el-table-column label="序号" type="index" align="center"></el-table-column>
        <el-table-column label="企业名称" prop="name" align="center"></el-table-column>
        <el-table-column label="企业编号" prop="id" width="200px" align="center"></el-table-column>
        <el-table-column label="联系人" prop="contact" width="200px" align="center"></el-table-column>
        <el-table-column label="联系电话" prop="contactNumber" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="primary" size="small" icon="el-icon-plus">添加合同</el-button>
            <el-button type="info" size="small" icon="el-icon-view" @click="handleShow(row)">查看企业</el-button>
            <el-button type="warning" size="small" icon="el-icon-edit" @click="handleEdit(row)">编辑企业</el-button>
            <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDelete(row)">删除企业</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[10, 20, 30, 40]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background></el-pagination>
    </el-card>
    <ent-detail v-else ref="entDetailRef" @back="showEntDetail=false"></ent-detail>
    <el-drawer :title="drawerTitle" :visible.sync="drawerVisible" @close="$refs.entFormRef.resetFormData()">
      <ent-form ref="entFormRef" @success="handleSuccess"></ent-form>
    </el-drawer>
  </div>
</template>

<style scoped lang="scss">
.enterprise {
  padding: 10px 0;

  .el-card {
    margin-bottom: 10px;
  }

  .el-table {
    margin: 10px 0;
  }

  .el-pagination {
    display: flex;
    justify-content: center;
  }
}
</style>
